﻿var LCA = LCA || {};

LCA.Unit = function () {

    var unitsData = [];
    // UnitId, UnitName, UnitGroupId

    return {
        init: function () {
            var req = {
                opt: "init_unit"
            };

            $.ajax({
                type: 'POST',
                url: '/Admin/Ajax/UnitService.aspx',
                data: JSON.stringify(req),
                contentType: 'application/json; charset=utf-8',
                success: function (request, msg) {
                    var obj = JSON.parse(request);

                    console.log('success: ');

                    LCA.Unit.initData(obj.Data);

                },
                error: function (request, status, error) {
                    console.log('error - UnitService.Unit');
                    // hideLoading();
                }

            });


        },
        initData: function (data) {
            unitsData = JSON.parse(data);
        },

        getAllUnits: function () {
            return unitsData;
        },

        getUnitsByUnitGroup: function (unitGroupId) {
            var arrResult = [];

            for (var i = 0; i < unitsData.length; i++) {
                var item = unitsData[i];
                if (item.UnitGroupId == unitGroupId) {
                    arrResult.push(item);
                }
            }


            return arrResult;
        }

    }
} ();


LCA.ItemUnit = function () {

    // this.data = [];

    var itemUnitsData = [];
    // ItemName, ItemType, ItemId, UnitGroupId, UnitId

    return {
        init: function () {
            var req = {
                opt: "init_item_unit"
            };

            $.ajax({
                type: 'POST',
                url: '/Admin/Ajax/UnitService.aspx',
                data: JSON.stringify(req),
                contentType: 'application/json; charset=utf-8',
                success: function (request, msg) {
                    var obj = JSON.parse(request);

                    LCA.ItemUnit.initData(obj.Data);

                },
                error: function (request, status, error) {
                    console.log('error - UnitService.ItemUnit');
                    // hideLoading();
                }

            });
        },

        initData: function (data) {
            itemUnitsData = JSON.parse(data);
            // this.data = JSON.parse(data);
        },

        getUnitNameByFlowId: function (flowId) {
            return "";
        },

        getUnitInfoByItem: function (item_type, item_id) {
            var result = { UnitGroupId: "", UnitId: "" } ;

            for (var i = 0; i < itemUnitsData.length; i++) {
                var item = itemUnitsData[i];
                if (item.ItemType == item_type && item.ItemId == item_id) {
                    result.UnitGroupId = item.UnitGroupId;
                    result.UnitId = item.UnitId;
                    break;
                }

            }

            return result;
        }


    }
} ();